package com.tianya.forum.services.impl;

import com.tianya.forum.common.AppResult;
import com.tianya.forum.common.ResultCode;
import com.tianya.forum.dao.BoardMapper;
import com.tianya.forum.exception.ApplicationException;
import com.tianya.forum.model.Board;
import com.tianya.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Slf4j
@Service
public class BoardService implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    /**
     * 显示前num个正常状态板块
     * @param num
     * @return
     */
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num <=0 ){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        List<Board> board = boardMapper.selectByNum(num);
        return board;
    }

    /**
     * 根据版块id使版块帖子数量+1
     * @param id
     */
    @Override
    public void addArticleCountById(Long id) {
        //参数校验
        if(id == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据版块id查询对应版块
        Board board = boardMapper.selectByPrimaryKey(id);
        int count = board.getArticleCount();
        //创建对象用于更新
        Board newboard = new Board();
        newboard.setId(board.getId());
        newboard.setArticleCount(count+1);
        int row = boardMapper.updateByPrimaryKeySelective(newboard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }


    /**
     * 根据版块ID查询
     * @param id
     * @return
     */
    @Override
    public Board selectById(Long id) {
        if( id == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }

    /**
     * 版块中的帖⼦数量 -1
     * @param id 版块Id
     */
    @Override
    public void subOneArticleCountById(Long id) {
        //参数校验
        if(id == null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString()+"222222222222222222");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //根据版块id查询对应版块
        Board board = boardMapper.selectByPrimaryKey(id);
        //创建对象用于更新
        Board newboard = new Board();
        newboard.setId(board.getId());
        newboard.setArticleCount(board.getArticleCount()-1);
        int row = boardMapper.updateByPrimaryKeySelective(newboard);
        if(row != 1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}


